package com.ruoyi.foodworks.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.foodworks.domain.WorksStatsVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.foodworks.domain.Works;
import com.ruoyi.foodworks.service.WorksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.Map;

/**
 * 作品详情Controller
 * 
 * @author lpt
 * @date 2025-08-12
 */
@RestController(value = "foodWorksController")
@RequestMapping("/works/works")
public class WorksController extends BaseController
{
    @Autowired
    private WorksService worksService;

    /**
     * 获取作品列表及统计信息（点赞数、评论数、作者信息）
     */
    @GetMapping("/listByAuthor")
    public AjaxResult listByAuthor(@RequestParam Integer authorId) {
        List<WorksStatsVO> list = worksService.selectWorksWithStatsByAuthorId(authorId);
        return AjaxResult.success(list);
    }

    /**
     * 获取所有作品列表及统计信息（点赞数、评论数、作者信息）
     */

    @GetMapping("/listAll") // 调整接口路径
    public AjaxResult listAll() { // 移除authorId参数
        List<WorksStatsVO> list = worksService.selectAllWorksWithStats();
        return AjaxResult.success(list);
    }

    /**
     * 查询作品详情列表
     */
    @PreAuthorize("@ss.hasPermi('works:works:list')")
    @GetMapping("/list")
    public TableDataInfo list(Works works)
    {
        startPage();
        List<Works> list = worksService.selectWorksList(works);
        return getDataTable(list);
    }

    /**
     * 导出作品详情列表
     */
    @PreAuthorize("@ss.hasPermi('works:works:export')")
    @Log(title = "作品详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Works works)
    {
        List<Works> list = worksService.selectWorksList(works);
        ExcelUtil<Works> util = new ExcelUtil<Works>(Works.class);
        util.exportExcel(response, list, "作品详情数据");
    }

    /**
     * 获取作品详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('works:works:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Integer workId)
    {
        return success(worksService.selectWorksByWorkId(workId));
    }

    /**
     * 新增作品详情
     */
    @PreAuthorize("@ss.hasPermi('works:works:add')")
    @Log(title = "作品详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Works works)
    {
        return toAjax(worksService.insertWorks(works));
    }

    /**
     * 修改作品详情
     */
    @PreAuthorize("@ss.hasPermi('works:works:edit')")
    @Log(title = "作品详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Works works)
    {
        return toAjax(worksService.updateWorks(works));
    }

    /**
     * 删除作品详情
     */
    @PreAuthorize("@ss.hasPermi('works:works:remove')")
    @Log(title = "作品详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Integer[] workIds)
    {
        return toAjax(worksService.deleteWorksByWorkIds(workIds));
    }

    /**
     * 审核作品详情
     */
    @PreAuthorize("@ss.hasPermi('works:works:audit')")
    @Log(title = "作品审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody Map<String, Object> auditData)
    {
        Integer workId = (Integer) auditData.get("workId");
        Integer status = (Integer) auditData.get("status");
        String remark = (String) auditData.get("remark");
        
        Works works = new Works();
        works.setWorkId(workId);
        
        if (status == 0) {
            // 拒绝：标记为删除状态
            works.setDel(1);
            works.setStatus(1); // 设置为草稿状态
        } else {
            // 通过：设置为正常状态
            works.setStatus(status);
        }
        
        return toAjax(worksService.updateWorks(works));
    }

    /**
     * 批量审核作品详情
     */
    @PreAuthorize("@ss.hasPermi('works:works:audit')")
    @Log(title = "批量作品审核", businessType = BusinessType.UPDATE)
    @PutMapping("/batchAudit")
    public AjaxResult batchAudit(@RequestBody Map<String, Object> auditData)
    {
        List<Integer> workIds = (List<Integer>) auditData.get("workIds");
        Integer status = (Integer) auditData.get("status");
        String remark = (String) auditData.get("remark");
        
        int result = 0;
        for (Integer workId : workIds) {
            Works works = new Works();
            works.setWorkId(workId);
            
            if (status == 0) {
                // 拒绝：标记为删除状态
                works.setDel(1);
                works.setStatus(1); // 设置为草稿状态
            } else {
                // 通过：设置为正常状态
                works.setStatus(status);
            }
            
            result += worksService.updateWorks(works);
        }
        
        return toAjax(result);
    }
}
